package com.editso.haier.result.commodity;

import com.editso.haier.entity.CommoditySecondary;
import com.editso.haier.entity.Secondary;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

import java.util.*;
import java.util.stream.Collectors;

@Getter
@Setter
@ToString
@AllArgsConstructor
public class DetailsSecondary {
    private String name;
    @JsonIgnore
    private Integer id;
    private Set<Item> values;

    @Data
    @AllArgsConstructor
    public static class Item{
        private String value;
        private Integer id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DetailsSecondary that = (DetailsSecondary) o;
        return Objects.equals(name, that.name) && that.getValues().addAll(this.getValues());
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    public static DetailsSecondary from(Secondary secondary){
        HashSet<Item> objects = new HashSet<>();
        objects.add(new Item(secondary.getValue(), secondary.getId()));
        return new DetailsSecondary(
                secondary.getName(),
                secondary.getId(),
                objects
        );
    }

    public static Set<DetailsSecondary> from(Iterable<CommoditySecondary> secondaries){
        HashSet<DetailsSecondary> objects = new HashSet<>();
        for (CommoditySecondary secondary : secondaries) {
            objects.add(from(secondary.getSecondary()));
        }
        return objects;
    }

}
